/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zyyoona7.picker;

import com.zyyoona7.picker.listener.PickerValueChange;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Picker;
import ohos.agp.components.AttrSet;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 描述 城市选择器
 *
 * @author wjt
 * @since 2021-04-12
 */
public class TwoCityPicker extends DirectionalLayout implements Picker.ValueChangedListener {
    /**
     * 选择值变更
     */
    private PickerValueChange pickerValueChange;

    private Context context;

    private static final int num7 = 7;
    private static final int num60 = 60;
    private static final int num70 = 70;
    private static final int num9 = 9;
    private static final int num50 = 50;
    private static final int num10 = 10;
    private List<CityEntity> p3List = new ArrayList<>(1);
    private List<List<CityEntity>> c3List = new ArrayList<>(1);
    private List<List<List<CityEntity>>> d3List = new ArrayList<>(1);
    private Picker province;
    private Picker city;
    private String provinceValue = "";
    private String cityValue = "";
    private String countyValue = "";
    private List<CityEntity> provinceEntityList = new ArrayList<>();
    private List<CityEntity> cityEntityList = new ArrayList<>();
    private List<CityEntity> countyEntityList = new ArrayList<>();
    private int yijixiabiao = 0;
    private int erjixiabiao = 0;
    private Picker.Formatter provinceFormatter;
    private Picker.Formatter cityFormatter;

    /**
     * 构造
     *
     * @param context
     */
    public TwoCityPicker(Context context) {
        super(context);
        this.context = context;
        initView(context);
    }

    /**
     * 构造
     *
     * @param context
     * @param attrSet
     */
    public TwoCityPicker(Context context, AttrSet attrSet) {
        super(context, attrSet);
        this.context = context;
        initTypeValue(context, attrSet);
        initView(context);
    }

    private void initView(Context context1) {
        Component component1 = LayoutScatter.getInstance(context1)
                .parse(ResourceTable.Layout_layout_two_picker_city, this, false);
        initLayout(component1);
        setOrientation(VERTICAL);
        addComponent(component1);
    }

    private void initLayout(Component component1) {
        /**
         * 省
         */
        province = (Picker) component1.findComponentById(ResourceTable.Id_two_province);
        province.setValueChangedListener(this::onValueChanged);
        /**
         * 市
         */
        city = (Picker) component1.findComponentById(ResourceTable.Id_two_city);
        city.setValueChangedListener(this::onValueChanged);

        ParseHelper.initThreeLevelCityList(context, p3List, c3List, d3List);
        p3List = Collections.synchronizedList(p3List);
        c3List = Collections.synchronizedList(c3List);
        d3List = Collections.synchronizedList(d3List);

        provinceValue = p3List.get(0).getName();
        cityValue = c3List.get(0).get(0).getName();
        countyValue = d3List.get(0).get(0).get(0).getName();
        provinceEntityList.clear();
        provinceEntityList.addAll(p3List);
        province.setValue(0);
        province.setMinValue(0);
        province.setMaxValue(provinceEntityList.size() - 1);
        provinceFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return provinceEntityList.get(i).getName();
            }
        };
        province.setFormatter(provinceFormatter);

        cityEntityList.clear();
        cityEntityList.addAll(c3List.get(yijixiabiao));
        city.setValue(0);
        city.setMinValue(0);
        city.setMaxValue(cityEntityList.size() - 1);
        cityFormatter = new Picker.Formatter() {
            @Override
            public String format(int i) {
                return cityEntityList.get(i).getName();
            }
        };
        city.setFormatter(cityFormatter);
    }

    /**
     * 初始化自定义属性
     *
     * @param context1
     * @param attrSet
     */
    private void initTypeValue(Context context1, AttrSet attrSet) {

    }

    private int getAttrSetInt(AttrSet attrs, String key, int def) {
        if (attrs.getAttr(key).isPresent()) {
            return attrs.getAttr(key).get().getIntegerValue();
        } else {
            return def;
        }
    }

    private float getAttrSetFloat(AttrSet attrs, String key, float def) {
        if (attrs.getAttr(key).isPresent()) {
            return attrs.getAttr(key).get().getFloatValue();
        } else {
            return def;
        }
    }

    private boolean getAttrSetBoolean(AttrSet attrs, String key, boolean isDef) {
        if (attrs.getAttr(key).isPresent()) {
            return attrs.getAttr(key).get().getBoolValue();
        } else {
            return isDef;
        }
    }

    private Color getAttrSetColor(AttrSet attrs, String key, Color color) {
        if (attrs.getAttr(key).isPresent()) {
            return attrs.getAttr(key).get().getColorValue();
        } else {
            return color;
        }
    }

    private String getAttrSetString(AttrSet attrs, String key, String str) {
        if (attrs.getAttr(key).isPresent()) {
            return attrs.getAttr(key).get().getStringValue();
        } else {
            return str;
        }
    }

    /**
     * picker值更改事件
     *
     * @param picker picker
     * @param i2 现在的下标
     * @param i1 即将选择的下标
     */
    @Override
    public void onValueChanged(Picker picker, int i2, int i1) {
        if (picker.getId() == ResourceTable.Id_two_province) {
            provinceValue = provinceEntityList.get(i1).getName();
            if (provinceValue.length() > num7) {
                picker.setSelectedTextSize(num60);
            } else {
                picker.setSelectedTextSize(num70);
            }
            cityEntityList.clear();
            cityEntityList.addAll(c3List.get(i1));
            cityValue = cityEntityList.get(0).getName();
            yijixiabiao = i1;
            city.setMinValue(0);
            city.setMaxValue(cityEntityList.size() - 1);
            city.setFormatter(null);
            city.setFormatter(cityFormatter);
            erjixiabiao = 0;
            if (pickerValueChange != null) {
                pickerValueChange.valueChange(provinceValue, cityValue, countyValue);
            }
        } else if (picker.getId() == ResourceTable.Id_two_city) {
            erjixiabiao = i1;
            countyEntityList.clear();
            countyEntityList.addAll(d3List.get(yijixiabiao).get(erjixiabiao));
            cityValue = cityEntityList.get(i1).getName();
            if (cityValue.length() > num9) {
                picker.setSelectedTextSize(num50);
            } else if (cityValue.length() > num7 && cityValue.length() < num10) {
                picker.setSelectedTextSize(num60);
            } else {
                picker.setSelectedTextSize(num70);
            }
            if (pickerValueChange != null) {
                pickerValueChange.valueChange(provinceValue, cityValue, countyValue);
            }
        }
    }

    /**
     * setValuChange
     *
     * @param pickerValueChange1
     */
    public void setValuChange(PickerValueChange pickerValueChange1) {
        this.pickerValueChange = pickerValueChange1;
    }

    /**
     * getProvinceValue
     *
     * @return ProvinceValue
     */
    public String getProvinceValue() {
        return provinceValue;
    }

    /**
     * setProvinceValue
     *
     * @param provinceValue
     */
    public void setProvinceValue(String provinceValue) {
        this.provinceValue = provinceValue;
    }

    /**
     * getCityValue
     *
     * @return CityValue
     */
    public String getCityValue() {
        return cityValue;
    }

    /**
     * setCityValue
     *
     * @param cityValue
     */
    public void setCityValue(String cityValue) {
        this.cityValue = cityValue;
    }

    /**
     * getCountyValue
     *
     * @return CountyValue
     */
    public String getCountyValue() {
        return countyValue;
    }

    /**
     * setCountyValue
     *
     * @param countyValue
     */
    public void setCountyValue(String countyValue) {
        this.countyValue = countyValue;
    }
}
